package com.maple.service.impl;

import com.maple.constant.StatusConstant;
import com.maple.entity.Orders;
import com.maple.mapper.DishMapper;
import com.maple.mapper.OrderMapper;
import com.maple.mapper.SetmealMapper;
import com.maple.mapper.UserMapper;
import com.maple.service.WorkSpaceService;
import com.maple.vo.BusinessDataVO;
import com.maple.vo.DishOverViewVO;
import com.maple.vo.OrderOverViewVO;
import com.maple.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;


@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getTodayBusinessData(LocalDateTime begin, LocalDateTime end) {
        
//        查询数据库
        Integer newUsers = userMapper.getUserCountByTimes(begin, end);
        Double turnover = orderMapper.getTurnoverCount(begin, end);
        Integer validOrderCount = orderMapper.getOrderCountByTimesAndStatus(begin, end, Orders.COMPLETED);
        Integer totalOrder = orderMapper.getOrderCountByTimesAndStatus(begin, end, null);
        Double orderCompletionRate = totalOrder.doubleValue() / validOrderCount;
        Double unitPrice = turnover / totalOrder;

//        封装对象并返回
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        Map<String, Integer> map = new HashMap<>();
        map.put("status", 0);
        Integer discontinued = setmealMapper.countByMap(map);
        map.put("status", 1);
        Integer sold = setmealMapper.countByMap(map);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        //获得当天的开始时间
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        //获得当天的结束时间
        LocalDateTime end = LocalDateTime.now().with(LocalTime.MAX);

        Integer allOrders = orderMapper.getOrderCountByTimesAndStatus(begin, end, null);
        Integer cancelledOrders = orderMapper.getOrderCountByTimesAndStatus(begin, end, Orders.CANCELLED);
        Integer completedOrders = orderMapper.getOrderCountByTimesAndStatus(begin, end, Orders.COMPLETED);
        Integer deliveredOrders = orderMapper.getOrderCountByTimesAndStatus(begin, end, Orders.CONFIRMED);
        Integer waitingOrders = orderMapper.getOrderCountByTimesAndStatus(begin, end, Orders.TO_BE_CONFIRMED);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }


}
